<% javascript 'filters' %>

<%= form_for @filter do |f| %>
  <%= base_errors_for f.object %>
  <ul class="nav nav-tabs" data-tabs="tabs">
    <li class="active"><a href="#primary" data-toggle="tab"><%= _("Filter") %></a></li>
    <% if show_location_tab? %>
      <li><a href="#locations" data-toggle="tab"><%= _("Locations") %></a></li>
    <% end %>
    <% if show_organization_tab? %>
      <li><a href="#organizations" data-toggle="tab"><%= _("Organizations") %></a></li>
    <% end %>
  </ul>

  <div class="tab-content">

    <div class="tab-pane active" id="primary">
      <% if @role.present? %>
        <%= field(f, :role_id, :label => _('Selected role')) do %>
          <%= @role.name %>
          <%= f.hidden_field :role_id, :value => f.object.role_id || @role.id %>
        <% end %>
      <% else %>
        <%= select_f(f, :role_id, Role.where(nil), :id, :name) %>
      <% end %>

      <%= selectable_f(f, :resource_type, Permission.resources_with_translations,
                       {:include_blank => _('(Miscellaneous)')}, {:'data-url' => permissions_path,
                                                                  :'data-allow-organizations' => @filter.allows_organization_filtering?,
                                                                  :'data-allow-locations' => @filter.allows_location_filtering?,
                                                                  :autofocus => true}) %>

      <%= multiple_selects f, :permissions, Permission.where(:resource_type => f.object.resource_type),
                           f.object.permissions.map(&:id), :label => _("Permission"), :required => true %>

      <div class="clearfix">
        <div class="controls" id="granular_warning" style="<%= f.object.granular? ? 'display:none' : '' %>">
          <div class="col-md-2"></div>
            <div class="col-sm-10 alert alert-info">
              <span class="pficon pficon-info"></span>
              <%= _("Selected resource type does not support granular filtering, therefore you can't configure granularity") %>
            </div>
        </div>
      </div>

      <div id="override_taxonomy_form" class="<%= f.object.allows_taxonomies_filtering? ? '' : 'hidden' %>">
        <%= checkbox_f f, :override?,
                       :label_help => _("Filters inherit organizations and locations associated with the role by default. If override field is enabled, <br> the filter can override the set of its organizations and locations. Later role changes will not affect such filter.<br> After disabling the override field, the role organizations and locations apply again.").html_safe,
                       :id => 'override_taxonomy_checkbox' %>
      </div>

      <div id="granular_form" style="<%= f.object.granular? ? '' : 'display:none' %>">
        <%= checkbox_f f, :unlimited?,
                       :label_help => _("If the unlimited field is enabled, the filter applies to all resources of the selected type. If the unlimited </br> field is disabled, you can specify further filtering using Foreman search syntax in the search field.</br> If the role is associated with organizations or locations, the filters are not considered unlimited</br> as they are scoped accordingly.").html_safe %>


        <%= autocomplete_f f, :search,
                           :disabled => f.object.unlimited?,
                           :full_path => search_path(f.object.resource_type)
        %>
      </div>
    </div>

    <% if show_location_tab? %>
      <div class="tab-pane" id="locations">
        <%= multiple_checkboxes f, :locations, f.object, User.current.my_locations, :label => _("Locations") %>
      </div>
    <% end %>

    <% if show_organization_tab? %>
      <div class="tab-pane" id="organizations">
        <%= multiple_checkboxes f, :organizations, f.object, User.current.my_organizations, :label => _("Organizations") %>
      </div>
    <% end %>
  </div>

  <%= submit_or_cancel f, false, :cancel_path => :back, :disabled => @role && @role.locked? %>

<% end %>
